package com.heriparid.slideshow;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import com.heriparid.slideshow.adapters.ImageSlideAdapter;
import com.heriparid.slideshow.model.PhotoHolder;
import com.heriparid.slideshow.paging.CustomViewPager;
import com.heriparid.slideshow.paging.PageManager;
import com.heriparid.slideshow.util.XmlResourceReader;

@SuppressLint("NewApi")
public class MainActivity extends SherlockActivity {
	
	private static final String TAG = MainActivity.class.getSimpleName();	
	
	private PageManager mPageManager;	
	private boolean isMenuShowed = false;
	private boolean isPlayedMenuItemShowed = true;
	private final List<PhotoHolder> mPhotoHolders = new ArrayList<PhotoHolder>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);
		requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
		
		getSupportActionBar().setTitle("Menu");
		
		reset();
		
		super.onCreate(savedInstanceState);
		
		initModels();
		
		setContentView(R.layout.activity_main);

		ImageSlideAdapter adapter = new ImageSlideAdapter();
				
		final CustomViewPager viewPager = (CustomViewPager) findViewById(R.id.vp_pager);		
		viewPager.setAdapter(adapter);		
		viewPager.setKeepScreenOn(true);
		viewPager.setPageMargin(10);		
		viewPager.setCurrentItem(PageManager.PAGE_MIDDLE, false);	
		
		mPageManager = new PageManager(this, viewPager, mPhotoHolders);
		mPageManager.init();	
	}
	
	private void initModels() {
		XmlResourceReader reader = new XmlResourceReader(this, mPhotoHolders);
		reader.parse();
	}
	
	@Override
	protected void onPause() {		
		super.onPause();
		reset();
	}
	
	@Override
	protected void onResume() {
		super.onResume();	
		mPageManager.playCurrentSound();
	}	
	
	private boolean isTouchEventBlocked = false;
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(isTouchEventBlocked && keyCode != KeyEvent.KEYCODE_MENU){
			Log.i(TAG, "isTouchEventBlocked");
			return true;
		}
		
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			if(!isMenuShowed){				
				getSupportActionBar().show();				
				isMenuShowed = true;
			}else{					
				getSupportActionBar().hide();
				isMenuShowed = false;
			}		
			
	        return true;
	    }
		return super.onKeyUp(keyCode, event);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
				
		Log.i(TAG, "onCreateOptionsMenu ");
		
		if (isPlayedMenuItemShowed) {
			menu.add(Menu.NONE, R.id.ID_ACTION_PLAY, Menu.NONE,
					R.string.action_lable_play)
					.setIcon(android.R.drawable.ic_media_play)
					.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		}else{		
			menu.add(Menu.NONE, R.id.ID_ACTION_STOP, Menu.NONE,
					R.string.action_lable_stop)
					.setIcon(android.R.drawable.ic_media_pause)
					.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		}
		
		menu.add(Menu.NONE, R.id.ID_ACTION_SETTINGS, Menu.NONE,
				R.string.action_lable_settings)
				.setIcon(android.R.drawable.ic_menu_preferences)
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

		return true;
	}
	
	private void doStartSlideshow(){
		mPageManager.startSlideShow();
		isPlayedMenuItemShowed = false;
		getSupportActionBar().hide();
		isMenuShowed = false;
		isTouchEventBlocked = true;
	}
	
	private void doStopSlideShow(){
		mPageManager.stopSlideShow();
		isPlayedMenuItemShowed = true;
		getSupportActionBar().hide();
		isMenuShowed = false;
		isTouchEventBlocked = false;
	}
	
	private void reset(){
		getSupportActionBar().hide();
		isTouchEventBlocked = false;
		isMenuShowed = false;
		isPlayedMenuItemShowed = true;
		if(mPageManager != null){
			mPageManager.stopSlideShow();
		}
		
		invalidateOptionsMenu();
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()){
		case R.id.ID_ACTION_PLAY:
			doStartSlideshow();
			break;
		case R.id.ID_ACTION_STOP:
			doStopSlideShow();
			break;
		case R.id.ID_ACTION_SETTINGS:			
			startActivity(new Intent(MainActivity.this, SettingActivity.class));
			getSupportActionBar().hide();
			isMenuShowed = false;			
			break;
		}
		
		invalidateOptionsMenu();
		return true;
		
	}
	
	@Override
	protected void onDestroy() {		
		super.onDestroy();
		mPageManager.clearResources();
	}
}
